//
//  SendHttpOperation.m
//  YOGProj
//
//  Created by liyun on 14-6-27.
//  Copyright (c) 2014年 liyun. All rights reserved.
//

#import "HCSendHttpOperation.h"
#import "ASIFormDataRequest.h"
#import "HCFileOperation.h"
#import "HCMacroDefinition.h"
#import "HCPublicStringConvert.h"
//static NSString *url_base = @"http://192.168.0.202:8088/OYPlatform/app/"; //外网地址
static NSString *url_base = @"http://51agile.xicp.net:8088/OYPlatform/app/";//公网地址

@implementation HCSendHttpOperation

+(HCSendHttpOperation *)shareSendHttpRequest
{
    static HCSendHttpOperation *instance = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate,^{
        instance = [[HCSendHttpOperation alloc]init];
    });
    return instance;
}

-(void)getDataWithUUID:(NSString *)uuid
{
    NSURL *urlWithUUID = [[NSURL alloc]initWithString:[NSString stringWithFormat:@"%@apprequest?uuid=%@",url_base,uuid]];
    NSLog(@"uuid-->%@",urlWithUUID);
    
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:urlWithUUID];
    [request setDelegate:self];
    [request setDidFinishSelector:@selector(urlRequestFinished:)];
    [request setDidFailSelector:@selector(urlRequestFail:)];
    request.userInfo = [[NSDictionary alloc]initWithObjectsAndKeys:uuid,@"uuid", nil];
//    NSLog(@"userInfo--->%@",request.userInfo);
    [request startAsynchronous];
}

-(void)urlRequestFinished:(ASIHTTPRequest *)request
{
    
    NSData       *data = [request responseData];
    NSDictionary *dic  = [NSDictionary dictionaryWithDictionary:[[HCPublicStringConvert shareHCPublicStringConvert] convertDic:data]];
    
//    NSLog(@"dic-->%@",dic);
    if (dic[@"success"])
    {
        NSLog(@"response-->%@",request.responseString);
        
        NSDate          *now       = [NSDate date];
        NSDateFormatter *formatter = [[NSDateFormatter  alloc ]  init];
        [formatter      setDateFormat:@"yyyy-MM-dd HH:mm"];
        NSString        *nowTime   = [formatter stringFromDate:now];

        NSString        *UUIDTypeList    = [[NSBundle mainBundle]pathForResource:@"UUIDTypeList" ofType:@"plist"];
        NSDictionary    *typeForUUID_Dic = [[NSDictionary alloc]initWithContentsOfFile:UUIDTypeList];
        int             typeForUUID      = [typeForUUID_Dic[request.userInfo[@"uuid"]] intValue];
        
        NSLog(@"nowTime-->%@",nowTime);
        
        switch (typeForUUID)
        {
            case TYPE_FOOD:
                //餐厅
                [[HCFileOperation shareFileOperation] saveSearchHistory:@{nowTime:dic[@"retData"][@"foods"]} type:TYPE_FOOD];
                
                break;
                
            case TYPE_SERVICE:
                //服务中心
                [[HCFileOperation shareFileOperation] saveSearchHistory:@{nowTime:dic[@"retData"]} type:TYPE_SERVICE];
                
                break;
                
//            case TYPE_EVENT:
//
//                 //文化活动
//                for (int i = 0; i<[dic[@"retData"][@"cep"] count]; i++)
//                {
//                    [[HCFileOperation shareFileOperation] saveSearchHistory:@{nowTime:dic[@"retData"][@"cep"][i]} type:TYPE_EVENT];
//                }
//                break;
            default:
                break;
        }
        [[NSNotificationCenter defaultCenter]postNotificationName:@"refreshTableView"
                                                           object:@{@"type":[NSString stringWithFormat:@"%d",typeForUUID]}];
    }
    else
        [self getDataWithUUID:request.userInfo[@"uuid"]];
}


-(void)urlRequestFail:(ASIHTTPRequest *)request
{
    [self                  getDataWithUUID:request.userInfo[@"uuid"]];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"TIP"
                                                        object:@{@"msg":@"网络获取数据失败，请重新尝试"}];
}

@end
